package com.le.tester.book.effectivejava3.focus18;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

/**
 * createTime：2025-06-18 13:49
 * description：组合优于类和类之间的继承
 */
public class InstrumentedHashSet<E> extends HashSet<E> {
    private int addCount = 0;

    public InstrumentedHashSet() {
    }

    public InstrumentedHashSet(int size, float loadFactor) {
        super(size, loadFactor);
    }

//    @Override
//    public boolean add(E e) {
//        addCount++;
//        return super.add(e);
//    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        addCount += c.size();
        return super.addAll(c);
    }

    public int getAddCount() {
        return addCount;
    }

    public static void main(String[] args) {
        InstrumentedHashSet<String> instance = new InstrumentedHashSet<>();
        instance.addAll(Arrays.asList("a", "b", "c"));
        System.out.println(instance.getAddCount());
    }
}
